package com.liwen.service.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author : li.rui
 * @time : 2017-05-13 14:42
 * @desc : app配置处理
 */
@Configuration
public abstract class AppConfig implements EnvironmentAware {
    private Logger log = LoggerFactory.getLogger(this.getClass());
    /**
     * 配置读取
     */
    private RelaxedPropertyResolver appConfigResolver;

    @Override
    public void setEnvironment(Environment environment) {
        this.appConfigResolver = new RelaxedPropertyResolver(environment, "app.");
    }

    /**
     * 获取前缀
     *
     * @return
     */
    abstract protected String prefix();

    /**
     * 组装配置项的key
     *
     * @param key
     * @return
     */
    private String key(String key) {
        return prefix() + key;
    }

    protected String getStr(String key) {
        return appConfigResolver.getProperty(key(key));
    }

    protected Integer getInt(String key) {
        return Integer.parseInt(appConfigResolver.getProperty(key(key)));
    }

    protected Long getLong(String key) {
        return Long.parseLong(appConfigResolver.getProperty(key(key)));
    }

    protected boolean getBool(String key) {
        return "true".equalsIgnoreCase(appConfigResolver.getProperty(key(key)));
    }

    protected void log(String msg) {
        log.info(msg);
    }
}
